package com.yonyou.iuap.jxzy.service;
import static com.yonyou.iuap.baseservice.intg.support.ServiceFeature.REFERENCE;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import nc.vo.pub.BusinessException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.yonyou.iuap.baseservice.intg.service.GenericIntegrateService;
import com.yonyou.iuap.baseservice.intg.support.ServiceFeature;
import com.yonyou.iuap.jxzy.dao.DomesticPaperMapper;
import com.yonyou.iuap.jxzy.entity.DomesticPaper;
import com.yonyou.iuap.jxzy.entity.PoundPictrueUrlEntity;
import com.yonyou.iuap.jxzy.utils.ExcelParam;
@Service

/**
 * DomesticPaper CRUD 核心服务,提供逻辑删除/乐观锁
 */
public class DomesticPaperService extends GenericIntegrateService<DomesticPaper>{


    private DomesticPaperMapper domesticPaperMapper;

    @Autowired
    public void setDomesticPaperMapper(DomesticPaperMapper domesticPaperMapper) {
        this.domesticPaperMapper = domesticPaperMapper;
        super.setGenericMapper( domesticPaperMapper);
    }
    


    /**
     * @CAU 可插拔设计
     * @return 向父类 GenericIntegrateService 提供可插拔的特性声明
     */
    @Override
    protected ServiceFeature[] getFeats() {
        return new ServiceFeature[]{ REFERENCE };
    }


	public void verify(List<DomesticPaper> listData,boolean isverify) throws BusinessException {
		if(!isverify){//校验是否已推单
			List<DomesticPaper> queryPushPound = this.domesticPaperMapper.queryPushPound(listData);
			if(queryPushPound.size()!=0){
				throw new BusinessException("选中的数据已有推单的单据");
			}
    	}
		for (DomesticPaper domesticPaper : listData) {
    		domesticPaper.setAuditStatus(isverify?"Y":"N");
		}
    	this.domesticPaperMapper.verify(listData);
    	this.domesticPaperMapper.saveS(listData);
	}


	/**
	 * 国内废纸称重导出
	 * @param jsonArray
	 * @return
	 */
	public ExcelParam download(JSONArray jsonArray,List<DomesticPaper> domlist) {
		List<DomesticPaper> list =null;
		if(jsonArray!=null){
			list = jsonArray.toJavaList(DomesticPaper.class);
		}else{
			list=domlist;
		}
		String[] heads = {"磅单号", "审核状态", "车牌号", "序列号", "主供货商", "辅供货商", "物料品种", "质量等级", "毛重", "皮重"
				,"货重", "含水分比", "含杂质比","水分加扣", "杂志加扣", "结算重量", "单价"
				,"价格系数", "是否人工检测", "单件重","总件数", "开包件数", "堆号", "检验人员"};
		List<String[]> data = new LinkedList<>();
		for (int i = 0; i < list.size(); i++) {
			DomesticPaper entity = list.get(i);
			String[] temp = new String[heads.length];
			temp[0] = entity.getPoundNo();
			if("Y".equals(entity.getAuditStatus())){
				temp[1] = "已审核";
			}else{
				temp[1] = "未审核";
			}
			temp[2] = entity.getLicensePlateNo();//车牌号
			temp[3] = entity.getSerialNo();//序列号
			temp[4] = entity.getMainSupplier();//主供货商
			temp[5] = entity.getAuxiliarySupplier();//辅供货商
			temp[6] = entity.getMaterialVariety();//物料品种
			
			temp[7] = entity.getQualityGrade();//质量等级
			temp[8] = entity.getGrossWeight();//毛重
			temp[9] = entity.getTareWeight();//皮重
			temp[10] = entity.getSettlementWeight();//货重
			temp[11] = String.valueOf(entity.getAverageWaterRatio());//含水分比
			temp[12] = String.valueOf(entity.getAverageImpurityRatio());//含杂质比
			temp[13] = entity.getAverageDewateringWeight();//水分另扣
			temp[14] = String.valueOf(entity.getAverageimpurityWeight());//杂志另扣
			
			temp[15] = entity.getNetWeight();
			temp[16] = entity.getAverageUnitPrice();
			temp[17] = entity.getPriceCoefficient();
			temp[18] = entity.getHumanVerifyFlag();//是否人工检测
			temp[19] = entity.getSinglePieceWeight();
			temp[20] = entity.getTotalPackageNo();
			temp[21] = entity.getUnpackNo();
			
			temp[22] = entity.getHeapName();
			temp[23] = entity.getInspecteds();
			data.add(temp);
		}
		ExcelParam param = new ExcelParam.Builder("国内导出数据").headers(heads).data(data).build();
		return param;
}	
	public int saveH(DomesticPaper domesticPaper) {
		int f=0;
//    		domesticPaper.
    	int saveH = this.domesticPaperMapper.saveH(domesticPaper);
    	int saveB = this.domesticPaperMapper.saveB(domesticPaper);
    	int saveA = this.domesticPaperMapper.saveA(domesticPaper);
    	List<DomesticPaper> arrayList = new ArrayList<DomesticPaper>();
    	arrayList.add(domesticPaper);
    	int saveM = this.domesticPaperMapper.saveM(arrayList);
    	f=saveH+saveB+saveA+saveM;
    	return f;
	}
	
	public int savePictureUrl(List<PoundPictrueUrlEntity> entitys){
		int f = 0;
		f = this.domesticPaperMapper.savePictureUrl(entitys);
		return f;
	}
    
}
